package edu.kit.aifb.ldstream.learner.query;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;

import edu.kit.aifb.ldstream.env.Environment;

class InstanceCacheIterator implements Iterator<URI>{

	private BufferedReader reader;
	private String nextInstance;

	InstanceCacheIterator(){
		try {
			File f = new File(Environment.INSTANCE_CACHE);
			f.setReadable(true);
			this.reader = new BufferedReader(new FileReader(f), 1024 * 1024);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		try {
			this.nextInstance = this.reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	@Override
	public boolean hasNext() {
		
		if(this.nextInstance == null){
			try {
				this.reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return false;
		}
		return true;
	}

	@Override
	public URI next() {
		if(hasNext()){
			String temp = this.nextInstance;
			try {
				this.nextInstance = this.reader.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return new URIImpl(temp);
		} 
		return null;
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException("Remove is not Supported");
	}

}
